package com.alibaba.nacos;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @ClassName: OpenlogicNacosApplication
 * @Description: Nacos 注册中心
 * @author weiyongpeng
 * @date 2025年2月4日 下午9:19:09
 */
@SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
@ServletComponentScan
@EnableScheduling
public class OpenLogicNacosApplication {

	/** 是否单机模式启动 */
	private static String standalone = "true";
	/** 是否开启鉴权 */
	private static String enabled = "false";

	public static void main(String[] args) {

		if (initEnv()) {
			ConfigurableApplicationContext context = SpringApplication.run(OpenLogicNacosApplication.class, args);
			getEnv(context);
		}
	}

	/**
	 * 初始化运行环境
	 */
	private static boolean initEnv() {
		System.setProperty("nacos.standalone", standalone);
		System.setProperty("nacos.core.auth.enabled", enabled);
		// 一旦Nacos初始化，用户名nacos将不能被修改，但你可以通过控制台或API来修改密码
		// https://nacos.io/en/blog/faq/nacos-user-question-history8420
//	    System.setProperty("nacos.core.auth.default.username", "nacos");
//	    System.setProperty("nacos.core.auth.default.password", "nacos");
		System.setProperty("server.tomcat.basedir", "logs");
		// 自定义启动端口号
		System.setProperty("server.port", "8848");
		return true;
	}

	public static void getEnv(ConfigurableApplicationContext application) {
		Environment env = application.getEnvironment();
		String name = env.getProperty("spring.application.name");
		String ip = null;
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		String port = env.getProperty("server.port");
		String context = env.getProperty("server.servlet.contextPath");
		System.out.println("\n\n\n\t\t" + name + "\n----------------------------------------------------------\n"
				+ "\t注册中心地址: \t http://" + ip + ":" + port + context + "\n"
				+ "----------------------------------------------------------\n");
	}
}
